package com.dqq.view.demo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

/**
 * Created by zhenxixianzai on 2015/5/5.
 */
public class RotateTextView extends TextView {
    private static final int DEFAULT_DEGREES = 0;
    // 0 代表不旋转 90 向右转90度
    private int mDegrees;
    private int defaultWidth;
    private int defaultHeight;

    private float textHeight;

    private final Rect rect = new Rect();

    private final float textScale = 0.9f;

    public RotateTextView(Context context) {
        super(context, null);
    }

    public RotateTextView(Context context, AttributeSet attrs) {
        super(context, attrs, android.R.attr.textViewStyle);
        this.setGravity(Gravity.CENTER);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RotateTextView);
        mDegrees = a.getInt(R.styleable.RotateTextView_degree,
                DEFAULT_DEGREES);
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (defaultWidth == 0 && MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY) {
            defaultWidth = getMeasuredWidth();
        }

        if (defaultHeight == 0 && MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY) {
            defaultHeight = getMeasuredHeight();
        }

        TextPaint textPaint = getPaint();
        String text = getText().toString();
        textPaint.getTextBounds(text, 0, text.length(), rect);

        Paint.FontMetrics fm = textPaint.getFontMetrics();
        // 因为字母上下高度不一样  统一算0.9
        textHeight = (int) Math.abs(fm.descent + fm.ascent);

        if (mDegrees % 180 == 0) {
            // 没有写宽度用字体占用宽度
            if (defaultWidth == 0) {
                defaultWidth = rect.width();
            }

            if (defaultHeight == 0) {
                defaultHeight = (int) (textHeight / textScale);
            }
        } else {
            // 没有写宽度用字体占用宽度
            if (defaultWidth == 0) {
                defaultWidth = (int) (textHeight / textScale);
            }

            if (defaultHeight == 0) {
                defaultHeight = rect.width();
            }
        }

        setMeasuredDimension(defaultWidth, defaultHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();

        TextPaint textPaint = getPaint();
        String text = getText().toString();
        // 居于布局中间旋转
        canvas.rotate(mDegrees, getWidth() / 2f, getHeight() / 2f);
        // 平移到布局中间  这样好计算  不然空间感不强  绘制位置不清楚在哪里
        canvas.translate(getWidth() / 2f, getHeight() / 2f);
        textPaint.getTextBounds(text, 0, text.length(), rect);

        // 宽度窄  高度长  底部左边为0,0
        // -rect.width() 将文字向下移动文字一半宽度
        // Math.abs(fm.descent + fm.ascent) * 0.9f / 2f 向右移半个文字高度
        canvas.drawText(text, -rect.width() / 2f, textHeight * textScale / 2f, textPaint);
        canvas.restore();
    }

    public void setDegrees(int degrees) {
        mDegrees = degrees;
        invalidate();
    }
}
